package util;

import java.util.Collection;
import java.util.Map;

import models.Category;
import models.State;
import play.Play;
import play.mvc.Scope.RenderArgs;

public class FindFoodUtil {

	public static boolean empty(String s) {
		return (s == null || s.trim().length() == 0);
	}

	public static boolean empty(Collection<?> c) {
		return (c == null || c.size() == 0);
	}

	public static boolean empty(Object[] a) {
		return (a == null || a.length == 0);
	}

	public static Factual getFactualClient() {
		String url = (String) Play.configuration.get("factual.url");
		String apiKey = (String) Play.configuration.get("factual.apiKey");
		String table = (String) Play.configuration.get("factual.table");
		return new Factual(url, table, apiKey);
	}

	public static void loadCategories(RenderArgs renderArgs) {
		renderArgs.put("categories", Category.values());
	}

	public static void loadStates(RenderArgs renderArgs) {
		renderArgs.put("states", State.values());
	}

	public static String lowerFirst(String string) {
		return Character.toLowerCase(string.charAt(0)) + string.substring(1);
	}

	public static String upperFirst(String string) {
		return Character.toUpperCase(string.charAt(0)) + string.substring(1);
	}

	public static Map<String, String> retypeMap(Map<String, Object> map) {
		try {
			Map<String, String> newMap = map.getClass().getConstructor()
					.newInstance();
			for (Map.Entry<String, Object> e : map.entrySet()) {
				if (e.getValue() != null) {
					newMap.put(e.getKey(), e.getValue().toString());
				} else {
					newMap.put(e.getKey(), null);
				}
			}
			return newMap;
		} catch (Exception e) {
			return null;
		}
	}
}
